package com.kamistoat.meimeichat.meimeichatclient.socket.handlers;

import com.kamistoat.meimeichat.meimeichatclient.application.UIServiceConnector;
import com.kamistoat.meimeichat.meimeichatclient.infrastructure.util.CacheUtil;
import com.kamistoat.meimeichat.meimeichatclient.socket.NettyClientBizHandler;
import com.kamistoat.meimeichat.meimeichatui.view.chat.IChatMethod;
import com.kamistoat.meimeichatserveraggrement.protocol.login.dto.ChatRecordDto;
import com.kamistoat.meimeichatserveraggrement.protocol.talk.TalkNoticeResponse;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import javafx.application.Platform;

import java.util.List;

/**
 * 当新开启一个聊天框时，接收服务器查到的聊天记录进行填充
 */
public class TalkNoticeHandler extends NettyClientBizHandler<TalkNoticeResponse> {

    public TalkNoticeHandler(UIServiceConnector uiServiceConnector) {
        super(uiServiceConnector);
    }

    /**
     * @param channel
     * @param talkNoticeResponse
     * @Warning:子类只需要重写该方法
     */
    @Override
    public void channelRead(Channel channel, TalkNoticeResponse talkNoticeResponse) {
        IChatMethod chat = uiServiceConnector.getIChatMethod();
        // 这里不是聊天通知，所以如果客户端又把打开的聊天框关上了，那就不用填充了。不像聊天那里必须通知到
        if (CacheUtil.talkMap.containsKey(talkNoticeResponse.getTalkId())) {
            Platform.runLater(() -> {
                switch (talkNoticeResponse.getTalkType()) {
                    // 好友
                    case 0:
                        List<ChatRecordDto> userRecordList = talkNoticeResponse.getChatRecordDtoList();
                        if (null == userRecordList || userRecordList.isEmpty()) return;
                        for (int i = userRecordList.size() - 1; i >= 0; i--) {
                            ChatRecordDto chatRecord = userRecordList.get(i);
                            //  自己的消息
                            if (0 == chatRecord.getMsgUserType()) {
                                chat.addTalkMsgRight(chatRecord.getTalkId(), chatRecord.getMsgContent(), chatRecord.getMsgType(), chatRecord.getMsgDate(), true, false, false);
                                continue;
                            }
                            // 好友的消息
                            if (1 == chatRecord.getMsgUserType()) {
                                chat.addTalkMsgUserLeft(chatRecord.getTalkId(), null, null, chatRecord.getMsgContent(), chatRecord.getMsgType(), chatRecord.getMsgDate(), true, false, false);
                            }
                        }
                        break;
                    // 群组
                    case 1:
                        List<ChatRecordDto> groupRecordList = talkNoticeResponse.getChatRecordDtoList();
                        if (null == groupRecordList || groupRecordList.isEmpty()) return;
                        for (int i = groupRecordList.size() - 1; i >= 0; i--) {
                            ChatRecordDto chatRecord = groupRecordList.get(i);
                            //  自己的消息
                            if (0 == chatRecord.getMsgUserType()) {
                                chat.addTalkMsgRight(chatRecord.getTalkId(), chatRecord.getMsgContent(), chatRecord.getMsgType(), chatRecord.getMsgDate(), true, false, false);
                                continue;
                            }
                            // 他人的消息
                            if (1 == chatRecord.getMsgUserType()) {
                                chat.addTalkMsgGroupLeft(chatRecord.getTalkId(), chatRecord.getSenderId(), chatRecord.getSenderNickName(), chatRecord.getSenderHead(), chatRecord.getMsgContent(), chatRecord.getMsgType(), chatRecord.getMsgDate(), true, false, false);
                            }
                        }
                        break;
                    default:
                        break;
                }
            });
        }
    }
}
